<template>
  <div class='login container'>
    <Header>
      <span>找回密码</span>
    </Header>
    <section>
      <div class='login-tel'>
        <input
          type="text"
          v-model='userPwd'
          placeholder="请输入新的密码"
        >
      </div>
      <div
        class='login-btn'
        @click='submitBtn'
      >确认</div>
    </section>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import Tabbar from "@/components/common/Tabbar.vue";
import Header from "@/views/login/Header.vue";
import { Toast } from "mint-ui";
import http from "@/common/api/request.js";
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      userPwd: "",
      //验证规则
      rules: {
        //手机号验证
        userPwd: {
          rule: /^\w{6,12}$/,
          msg: "密码不能为空,并且是6-12位",
        },
      },
    };
  },
  components: {
    Header,
    Tabbar,
  },
  methods: {
    ...mapMutations(["userLogin"]),

    //点击登录方法
    login() {
      //前端验证  validate方法
      //验证通过后发送请求
      //发送请求，后端验证
      http
        .$axios({
          url: "/api/login",
          method: "POST",
          //get 是params: post是data:
          data: {
            userTel: this.$route.query.phone,
            userPwd: this.userPwd,
          },
        }) //上面是发送的，下面是接收的 //res就是index的data返回的数据  res是约定的写法？
        .then((res) => {
          //提示信息
          Toast(res.msg);
          //登录失败
          //console.log(res);//输出所有信息
          //success和succcess导致 if永远是错的，！if就是对的，永远return 导致后面的console无法输出
          if (!res.success) {
            return;
          }
          //登录成功 ==》跳转页面，存储用户信息
          //console.log(res);//下面的即使跳转了，数据仍在res.data里面
          //需要将这些数据保存到vuex里面！！！！
          this.userLogin(res.data); //=》这里就执行了存储
          //跳转到我的页面中
          this.$router.push("/my");
        });
    },

    //...mapMutations(["USER_LOGIN"]),

    //点击确认
    submitBtn() {
      if (!this.validate("userPwd")) return;
      //确认修改
      http
        .$axios({
          //axios请求
          url: "/api/recovery", //这个是后端了
          method: "POST",
          data: {
            //this.$router.push
            phone: this.$route.query.phone, //这个上一层取到的 关注
            pwd: this.userPwd,
          },
        })
        .then((res) => {
          if (res.success) {
            Toast("修改成功");
            //this.$router.push({
            //这个是push到前端的页面用push
            //path: "/login",
            //});
            //登录成功 ==》跳转页面，存储用户信息
            //this.USER_LOGIN(res.data);
            //this.userLogin(res.data);
            this.login();
            //跳转到我的页面中
            //this.$router.push("/my");
          } else {
            Toast("修改失败,请重新尝试");
          }
        });
    },
    /*
			this.$router.push({
            	name: "btn",
            		query: {
              		phone: this.userTel,
            		}, 
			*/
    //验证信息提示
    validate(key) {
      let bool = true;
      if (!this.rules[key].rule.test(this[key])) {
        //提示信息
        Toast(this.rules[key].msg);
        bool = false;
        return false; //退出这一层循环
      }
      return bool;
    },
  },
};
</script>

<style scoped lang='scss'>
section {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #f5f5f5;
  div {
    margin: 0.266666rem 0;
    width: 8.933333rem;
    height: 1.173333rem;
  }
  input {
    box-sizing: border-box;
    padding: 0 0.266666rem;
    line-height: 1.173333rem;
    background-color: #ffffff;
    border: 1px solid #ccc;
    border-radius: 6px;
  }
  .login-tel {
    margin-top: 0.8rem;
    input {
      width: 8.933333rem;
    }
  }
  .login-code {
    display: flex;
    input {
      flex: 1;
    }
    button {
      padding: 0 0.533333rem;
      line-height: 1.173333rem;
      color: #fff;
      background-color: #b0352f;
      border: 0;
      border-radius: 6px;
    }
  }
  .login-btn {
    line-height: 44px;
    color: #fff;
    text-align: center;
    background-color: #b0352f;
    border-radius: 6px;
  }
}
</style>
